#test_lib_include = [spice_server_include, include_directories('.')]
#test_lib_deps = [spice_server_deps, spice_common.get_variable('gio2_deps')]
#if host_machine.system() != 'windows'
#  test_lib_deps += [dependency('gio-unix-2.0')]
#endif

project(spice-server-test LANGUAGES CXX)

set(test_lib_sources
  basic-event-loop.c
  basic-event-loop.h
  test-display-base.cpp
  test-display-base.h
  test-glib-compat.h
  win-alarm.c
  win-alarm.h
  vmc-emu.cpp
  vmc-emu.h
  )

#test_libs = []
#test_libs += static_library('testlib', test_lib_sources,
#                            link_with: spice_server_static_lib,
#                            include_directories : test_lib_include,
#                            dependencies : test_lib_deps,
#                            install : false)

add_library(testlib
    STATIC
    ${test_lib_sources}
    )
target_include_directories(testlib
    PRIVATE
         ${spice_server_include}
    )
target_link_libraries(testlib
    PUBLIC
        spice-server
    )

#
#stat_test_libs = {'testlib_stat1' : ['1', '0', '0'],
#                  'testlib_stat2' : ['2', '0', '1'],
#                  'testlib_stat3' : ['3', '1', '0'],
#                  'testlib_stat4' : ['4', '1', '1']}
#foreach lib, params : stat_test_libs
#  test_libs += static_library(lib, 'stat-test.c',
#                              c_args : ['-DTEST_NAME=stat_test@0@'.format(params[0]),
#                                        '-DTEST_COMPRESS_STAT=@0@'.format(params[1]),
#                                        '-DTEST_RED_WORKER_STAT=@0@'.format(params[2])],
#                              include_directories : test_lib_include,
#                              dependencies : test_lib_deps,
#                              install : false)
#endforeach

add_library(testlib_stat1
    STATIC
        stat-test.c
    )
target_link_libraries(testlib_stat1
    PUBLIC
    spice-server
    )
target_compile_definitions(testlib_stat1
    PRIVATE -DTEST_NAME=stat_test1 -DTEST_COMPRESS_STAT=0 -DTEST_RED_WORKER_STAT=0
    )

add_library(testlib_stat2
    STATIC
        stat-test.c
    )
target_link_libraries(testlib_stat2
    PUBLIC
    spice-server
    )
target_compile_definitions(testlib_stat2
    PRIVATE -DTEST_NAME=stat_test2 -DTEST_COMPRESS_STAT=0 -DTEST_RED_WORKER_STAT=1
    )

add_library(testlib_stat3
    STATIC
        stat-test.c
    )
target_link_libraries(testlib_stat3
    PUBLIC
    spice-server
    )
target_compile_definitions(testlib_stat3
    PRIVATE -DTEST_NAME=stat_test3 -DTEST_COMPRESS_STAT=1 -DTEST_RED_WORKER_STAT=0
    )

add_library(testlib_stat4
    STATIC
        stat-test.c
    )
target_link_libraries(testlib_stat4
    PUBLIC
    spice-server
    )
target_compile_definitions(testlib_stat4
    PRIVATE -DTEST_NAME=stat_test4 -DTEST_COMPRESS_STAT=1 -DTEST_RED_WORKER_STAT=1
    )

#
#tests = [
#  ['test-codecs-parsing', true],
#  ['test-dispatcher', true, 'cpp'],
#  ['test-options', true],
#  ['test-stat', true],
#  ['test-agent-msg-filter', true],
#  ['test-loop', true],
#  ['test-qxl-parsing', true, 'cpp'],
#  ['test-leaks', true],
#  ['test-vdagent', true],
#  ['test-fail-on-null-core-interface', true],
#  ['test-empty-success', true],
#  ['test-channel', true, 'cpp'],
#  ['test-stream-device', true, 'cpp'],
#  ['test-set-ticket', true],
#  ['test-listen', true],
#  ['test-record', true],
#  ['test-display-no-ssl', false],
#  ['test-display-streaming', false],
#  ['test-playback', false],
#  ['test-display-resolution-changes', false],
#  ['test-two-servers', false],
#  ['test-display-width-stride', false],
#]


set(tests_srcs
  test-codecs-parsing.c
#  test-dispatcher.cpp
  test-options.c
#  test-stat.c
  test-agent-msg-filter.c
  test-loop.c
#  test-qxl-parsing.cpp
  test-leaks.c
  test-vdagent.c
  test-fail-on-null-core-interface.c
  test-empty-success.c
#  test-channel.cpp
#  test-stream-device.cpp
  test-set-ticket.c
  test-listen.c
  test-record.c
#  test-display-no-ssl.c
#  test-display-streaming.c
#  test-playback.c
#  test-display-resolution-changes.c
#  test-two-servers.c
#  test-display-width-stride.c
  )
#
##if spice_server_has_sasl
##  tests += [['test-sasl', true]]
##endif
#if(${spice_server_has_sasl})
#    set(tests_srcs ${tests_srcs} test-sasl.c)
#endif()
#
##
##if spice_server_has_smartcard == true
##  tests += [['test-smartcard', true, 'cpp']]
##endif
#if(${spice_server_has_smartcard})
#    set(test_srcs ${tests_srcs} test-smartcard.cpp)
#endif()
#
##if host_machine.system() != 'windows'
##  tests += [
##    ['test-stream', true],
##    ['test-stat-file', true],
##    ['test-websocket', false],
##  ]
##endif
#
#if (!WIN32)
#    set(tests_srcs ${tests_srcs}
#    test-stream.c
#    test-stat-file.c
#    test-websocket.c
#    )
#endif()
#
##
##if spice_server_has_gstreamer
##  tests += [['test-gst', false, 'cpp']]
##  if get_option('extra-checks')
##    test('test-video-encoders', files('test-video-encoders'))
##  endif
##endif
#if (${spice_server_has_gstreamer})
#        set(tests_srcs ${tests_srcs} test-gst.cpp)
#        if (${ENABLE_EXTRA_CHECKS_PS})
##                test('test-video-encoders', files('test-video-encoders'))
#        endif()
#endif()

#
#foreach t : tests
#  test_name = t[0]
#  is_test = t[1]
#  if t.length() > 2
#    language = t[2]
#  else
#    language = 'c'
#  endif
#  exe = executable(test_name,
#                   sources : '@0@.@1@'.format(test_name, language),
#                   link_with : test_libs,
#                   include_directories : test_lib_include,
#                   c_args : ['-DSPICE_TOP_SRCDIR="@0@"'.format(meson.source_root())],
#                   cpp_args : ['-DSPICE_TOP_SRCDIR="@0@"'.format(meson.source_root())],
#                   dependencies : test_lib_deps,
#                   install : false)
#
#  if is_test
#    test(test_name, exe)
#  endif
#endforeach

find_package(MyGlib2 REQUIRED)
find_package(MyOpenSSL REQUIRED)
find_package(MyPixman REQUIRED)

function (add_my_test TEST_SOURCE_FILE)
        get_filename_component (test_name ${TEST_SOURCE_FILE} NAME_WE)
        add_executable (${test_name} ${TEST_SOURCE_FILE})
#        target_include_directories(${test_name} PRIVATE ${spice_common_include})
        target_link_libraries (${test_name} PRIVATE
            spice-server
            testlib
            ${GLIB2_LIBRARIES}
            ${SSL_LIBRARY}
            ${CRYPTO_LIBRARY}
            ${PIXMAN_LIBRARY}
            )
        target_compile_definitions(${test_name}
            PUBLIC SPICE_TOP_SRCDIR="${CMAKE_CURRENT_SOURCE_DIR}/../.."
            )
        add_test (NAME ${test_name}
          COMMAND ${test_name})
endfunction (add_my_test)

foreach (test_file ${tests_srcs})
        add_my_test (${test_file})
endforeach()

#
#executable('spice-server-replay',
#           sources : ['replay.c', join_paths('..', 'event-loop.c'), 'basic-event-loop.c', 'basic-event-loop.h'],
#           link_with : spice_server_shared_lib,
#           include_directories : test_lib_include,
#           dependencies : test_lib_deps,
#           install : false)
#


